package com.gusha.service;

import java.util.ArrayList;

import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.ScrollPictureDao;
import com.gusha.model.ScrollPicture;

@Service
@Transactional
public class ScrollPictureService {
	
	@Autowired
	private ScrollPictureDao scrollPictureDao;
	
	
	public Page<ScrollPicture> findAll(Pageable pageable, ScrollPicture scrollPicture) {

        @SuppressWarnings("serial")
        Page<ScrollPicture> result = scrollPictureDao.findAll(new Specification<ScrollPicture>() {
            @Override
            public Predicate toPredicate(Root<ScrollPicture> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (scrollPicture.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),scrollPicture.getId()));
                }
                if (scrollPicture.getName() != null) {
                    list.add(cb.equal(root.get("name").as(String.class),scrollPicture.getName()));
                } if (scrollPicture.getPath() != null) {
                    list.add(cb.equal(root.get("path").as(String.class),scrollPicture.getPath()));
                } if (scrollPicture.getPath() != null) {
                    list.add(cb.equal(root.get("path").as(String.class),scrollPicture.getPath()));
                }if (scrollPicture.getSubject() != null) {
                    list.add(cb.equal(root.get("subject").as(String.class),scrollPicture.getSubject()));
                } if (scrollPicture.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),scrollPicture.getUpdateTime()));
                } if (scrollPicture.getAddTime() != null) {
                    list.add(cb.equal(root.get("addTime").as(Date.class),scrollPicture.getAddTime()));
                } if (scrollPicture.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),scrollPicture.getIsDelete()));
                }
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }
}
